var CalUtil = function (precision){
	this.value=0;
	var reg =/^[0-9]$/;
	if(typeof(precision)=='number'){
		if(!reg.test(precision)){
			throw Error("精度需为0-9位数");
		}else{
			this.precision=precision;
		}
	}else{
		this.precision=null;
	}
	return this;
}

CalUtil.prototype={
	constructor: CalUtil,
	add: function(...vars) { 
		this.cal("add",vars);
		return this;
	},
	sub: function(...vars) { 
		this.cal("sub",vars);
		return this;
	},
	mul: function(...vars) {
		this.cal("mul",vars);
		return this;
	},
	div:function(...vars){
		this.cal("div",vars);
		return this;
	},
	cal:function(type,vars){
		var calvars =vars;
		var first = this.checkType(calvars[0]);
		for(var ind=1;ind<calvars.length;ind++){
			if(type=="add"){
				first+=this.checkType(calvars[ind]);
			}
			if(type=="sub"){
				first-=this.checkType(calvars[ind]);
			}
			if(type=="mul"){
				first*=this.checkType(calvars[ind]);
			}
			if(type=="div"){
				first/=this.checkType(calvars[ind]);
			}
		}
		this.value = first;
		if(this.precision){
			this.value = Number(this.value.toFixed(this.precision));
		}
		return this;
	},
	checkType:function(var2){
		if(var2 instanceof CalUtil){
			return var2.value;
		}
		if(typeof(var2)!='number'){
			throw Error("参数应为数字类型");
		}
		return var2;
	},
	setScale:function(precision){
		this.value = Number(this.value.toFixed(precision));
		return this;
	},
}